package com.zhuizikejithreeig.cn.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhuizikejithreeig.cn.threeigkeji.R;


public class SimpleCardFragment extends Fragment {
    private String       mTitle;
    private ImageView    mImageView;
    private TextView     mTextButton;
    private LinearLayout mLinearLayout;
    private String       uri;
    private String       mac;
    private ProgressBar  mProgressBar;


    //这是单例模式,懒汉式
    public static SimpleCardFragment getInstance(String title, String uri, String mac) {
        SimpleCardFragment sf = new SimpleCardFragment();
        sf.mTitle = title;
        sf.uri = uri;
        sf.mac = mac;
        return sf;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fr_simple_card, null);
        mImageView = (ImageView) v.findViewById(R.id.image);
        mTextButton = (TextView) v.findViewById(R.id.text_button);
        mLinearLayout = (LinearLayout) v.findViewById(R.id.linelayout);
        mProgressBar = (ProgressBar) v.findViewById(R.id.progressview);


        if (TextUtils.equals(mTitle, "0")) {
            mImageView.setVisibility(View.GONE);
            mLinearLayout.setVisibility(View.VISIBLE);
            mTextButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (TextUtils.equals(mTextButton.getText().toString().trim(), "开始按摩")) {
                        mTextButton.setText("暂停按摩");
                    } else {
                        mTextButton.setText("开始按摩");
                    }
                }
            });
        } else {


            if (TextUtils.isEmpty(uri)){
                mProgressBar.setVisibility(View.VISIBLE);
                mImageView.setVisibility(View.GONE);
            }else {
                mProgressBar.setVisibility(View.GONE);
                mImageView.setVisibility(View.VISIBLE);
                Glide.with(getActivity()).
                        load(uri).
                        asBitmap().
                        //                    placeholder(R.mipmap.laoding).//加载中显示的图片
                                error(R.mipmap.eeror).//加载失败时显示的图片
                        into(mImageView);
            }

        }



        return v;
    }
}